package com.example.csp_karaokemobile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.csp.dao.pojo.TheLoaiGame;
import vn.csp.util.JSONParser;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	}
	public void xuli(View view) {
		
		layDuLieu();
	}
	
	/** Danh sách thể loại game ./. */
	List<TheLoaiGame> danhSachTheLoaiGame = new ArrayList<TheLoaiGame>();
	/**
	 * Lấy danh sách thể loại game
	 */
    public void layDuLieu() {
		new Thread(){
			@Override
			public void run() {
				JSONParser jsonParser = new JSONParser();
		        try {
		        	String url = "http://172.16.5.199/csp_karaoke/karaoke_services.php";
		        	String[][] param = new String[2][2];
		        	param[0][0] = "a"; // $_POST['a] - > a
		        	param[0][1] = "a";

		        	JSONObject jsonObject = jsonParser.getJSONObject(url, param);
		        	JSONArray androidArray = jsonObject.getJSONArray("JSONResponse");
		        	for (int i = 0; i < androidArray.length(); i++) {
		        		danhSachTheLoaiGame.add(new TheLoaiGame(
			    				androidArray.getJSONObject(i).getString("TLG_ID"), 
			    				androidArray.getJSONObject(i).getString("TLG_TEN_THE_LOAI"), 
			    				androidArray.getJSONObject(i).getString("TLG_MO_TA")));
//		        		danh_sach_the_loai_game.add(androidArray.getJSONObject(i).getString("TLG_TEN_THE_LOAI"));
		        		// Lưu danh sách game vì trí trong ListView <=> vị trí trong CSDL (TLG_ID)
//		        		danh_sach_the_loai_game_id.put(i, androidArray.getJSONObject(i).getString("TLG_ID"));
					}
		        	Message m = myHandler.obtainMessage();
					myHandler.sendMessage(m);
				} catch (Exception e) {
					e.printStackTrace();
					Message m = myHandler.obtainMessage();
					Bundle b = new Bundle();
					b.putString("thong_diep_loi", "Vui lòng kiểm tra kết nối.");
					m.setData(b);
					myHandler.sendMessage(m);
				}
			}
		}.start();
	}
    Handler myHandler = new Handler() {
		public void handleMessage (Message m) {
			String thong_diep_loi = m.getData().getString("thong_diep_loi");
			if(thong_diep_loi!=null){
				Toast.makeText(getApplicationContext(),thong_diep_loi, Toast.LENGTH_SHORT).show();
			}else{
				if (!danhSachTheLoaiGame.isEmpty()){
					List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
					//http://wptrafficanalyzer.in/blog/android-listfragment-with-images-and-text-using-android-support-library/
					int count = 0;
					Enumeration<TheLoaiGame>  e = Collections.enumeration(danhSachTheLoaiGame);
					while(e.hasMoreElements()){
						TheLoaiGame tlg = e.nextElement();
//			            HashMap<String, String> hm = new HashMap<String,String>();
//			            hm.put("the_loai",tlg.getTLG_TEN_THE_LOAI());
//			            aList.add(hm);
						Log.i("Dau ra",tlg.getTLG_TEN_THE_LOAI());
					  }
				}
			}
		}
	};
}
